package com.gitee.huanminabc.fastboot.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONWriter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.gitee.huanminabc.fastboot.web.context.ApiThreadContext;
import com.gitee.huanminabc.nullchain.common.NullResult;
import lombok.extern.slf4j.Slf4j;

/**
 * 简要描述
 *
 * @Author: huanmin
 * @Date: 2025/4/12 01:02
 * @Version: 1.0
 * @Description: 文件作用详细描述....
 */
@Slf4j
public class ResponseBodyHandlerInterceptor implements HandlerInterceptor {
    @Override
    public NullResult<?> postHandle(NullResult<?> result) throws Exception {
        if (result.isSuccess()){
            String responseBody = JSON.toJSONString(result.getBody(), SerializerFeature.PrettyFormat, SerializerFeature.IgnoreNonFieldGetter);
            //限制返回值长度1024
            if (responseBody.length() > 1024) {
                responseBody = responseBody.substring(0, 1024) + "......内容被截断";
            }
            log.info("{}方法返回值:{}", ApiThreadContext.get().getUrl(), responseBody); //WriteMapNullValue:输出空置字段
        }else{
            log.error("{}方法返回值:{}", ApiThreadContext.get().getUrl(), result.getMessage());
        }
        return HandlerInterceptor.super.postHandle(result);
    }
}
